package com.itheima.service.impl;

import com.itheima.pojo.Question;
import com.itheima.repository.QuestionRepository;
import com.itheima.service.QuestionService;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
public class QuestionServiceImpl implements QuestionService {

    @Autowired
    private QuestionRepository questionRepository;

    /**
     * @Author: Administrator
     * @Date: 16:58 2021/2/4
     * @Parms [question]
     * @ReturnType: void
     * @Description: 添加问答
    */
    @Override
    public void addQuestion(Question question) {
        String s = UUID.randomUUID().toString().replace("-","");
        question.setId(s);
        questionRepository.save(question);
    }

    /**
     * @Author: Administrator
     * @Date: 16:58 2021/2/4
     * @Parms [title]
     * @ReturnType: java.util.List<java.lang.String>
     * @Description: 根据问题查询答案
    */
    @Override
    public  List<String> findAnswerByTitle(String title) {
        QueryBuilder queryBuilder = QueryBuilders.matchQuery("title", title);
        Iterable<Question> titles = questionRepository.search(queryBuilder);
        List<String> answers = new ArrayList<>();
        for (Question question : titles) {
            System.out.println(question);
           answers .add(question.getAnswer());
        }
        return answers;
    }
}
